package com.example.treehole;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.base.BindAdapter;
import com.example.treehole.bean.Chat;
import com.example.treehole.bean.Post;
import com.example.treehole.bean.User;
import com.example.treehole.databinding.ActivityUserPageBinding;
import com.example.treehole.databinding.ItemPostBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;

import java.util.HashMap;
import java.util.List;

public class UserPageActivity extends BaseBindingActivity<ActivityUserPageBinding> {

    private BindAdapter<ItemPostBinding, Post> postAdapter = new BindAdapter<ItemPostBinding, Post>() {
        @Override
        public ItemPostBinding createHolder(ViewGroup parent) {
            return ItemPostBinding.inflate(getLayoutInflater(), parent, false);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void bind(ItemPostBinding itemPostBinding, Post post, int position) {
            itemPostBinding.getRoot().setOnClickListener(view -> {
                startActivity(PostDetailActivity.class, intent -> {
                    intent.putExtra("post", post);
                });
            });
            loadImage(post.face, itemPostBinding.ivFace);
            itemPostBinding.tvUpTime.setText(post.time);
            itemPostBinding.tvUpName.setText(post.nickname);
            itemPostBinding.title.setText(post.title);
            if (!post.file.isEmpty()) {
                itemPostBinding.ivImage.setVisibility(View.VISIBLE);
                loadImage(post.file, itemPostBinding.ivImage);
            } else {
                itemPostBinding.ivImage.setVisibility(View.GONE);
            }

        }
    };

    @Override
    protected void initListener() {
        viewBinder.tvChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> params = new HashMap<>();
                params.put("userid1", App.user.id);
                params.put("userid2", user.id);
                HttpUtil.getModel(Api.CHAT_GET_BY_ID, params, Chat.class, new HttpUtil.OnNetModelListener<Chat>() {
                    @Override
                    public void success(Chat data, String message) {
                        startActivity(ChatActivity.class, intent -> intent.putExtra("chat", data));
                    }

                    @Override
                    public void error(String message) {

                    }
                });
            }
        });

    }

    User user;

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        user = (User) getIntent().getSerializableExtra("user");
        viewBinder.tvNickname.setText(user.nickname.isEmpty() ? user.phone : user.nickname);
        viewBinder.tvTitle.setText((user.nickname.isEmpty() ? user.phone : user.nickname) + "的主页");
        if (user.age != 0) {
            viewBinder.tvAge.setText(user.age);
        }
        if (user.info != null && !user.info.isEmpty()) {
            viewBinder.tvInfo.setText(user.info);
        }
        if (user.address != null && !user.address.isEmpty()) {
            viewBinder.tvAddress.setText(user.address);
        }
        loadImage(user.face, viewBinder.ivAvatar);
        viewBinder.rvWorks.setAdapter(postAdapter);
        getPostData();
    }

    private void getPostData() {
        showLoading();
        HashMap<String, Object> params = new HashMap<>();
        params.put("userid", user.id);
        HttpUtil.getList(Api.GET_MY_POST, params, Post.class, new HttpUtil.OnNetListListener<Post>() {
            @Override
            public void success(List<Post> data, String message) {
                dismissLoading();
                postAdapter.getData().clear();
                postAdapter.getData().addAll(data);
                postAdapter.notifyDataSetChanged();
            }

            @Override
            public void error(String error) {
                toast(error);
                dismissLoading();
            }
        });
    }


}